
/**
 * 手机号15689003415--》156 8900 3415
 * 手机号15689003415--》156-8900-3415
 */
export function formatPhoneNum (val, rules) {
  val = val.toString()
  let tagString = '';
  if (rules) {
    tagString = val.replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1' + rules + '$2' + rules + '$3')
  } else {
    tagString = val.replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3')
  }
  return tagString
}
/*
*转换资金千分位
*/
export function transNum (str) {
  var num = Number(str);
  if (isNaN(num)) {
    return str;
  } else {
    num = num.toFixed(3).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
    return num;
  }
}
/*
*去除千分位
*/
export function delcommafy (num) {
  if (!num) return num;
  num = num.toString();
  num = num.replace(/,/gi, "");
  return Number(num)
}
/*
*数字补零
*/
export function returnFloat (value) {
  var value = Math.round(parseFloat(value) * 100) / 100
  var xsd = value.toString().split('.')
  if (xsd.length == 1) {
    value = value.toString() + '.00'
    return value
  }
  if (xsd.length > 1) {
    if (xsd[1].length < 2) {
      value = value.toString() + '0'
    }
    return value
  }
}
/**
 * 过滤对象中为空的属性
 */
export function filterObj (obj) {
  if (!(typeof obj == 'object')) {
    return;
  }
  for (let key in obj) {
    if (obj.hasOwnProperty(key)
      && (obj[key] == null || obj[key] == undefined || obj[key] === '')) {
      delete obj[key];
    }
  }
  return obj;
}
/**
 * 深拷贝
 */
export function deepClone (source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach((keys) => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

